package com.medical.medical.controller.lsyController;


import com.alibaba.fastjson.JSONArray;
import com.medical.medical.service.DepartmentService;
import com.medical.medical.service.WorkForcesService;
import com.medical.medical.util.Constants;
import com.medical.medical.util.PageSupport;
import com.medical.medical.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @authorDesc 诸神在上，佑我程序，玄功护体，BUG皆去！
 * @author LiSuYang
 * @Date 2022-09-14 09:32:53
 * @version 1.0.0
 * @description 排班表-信息请求层
 */
@Controller
@Slf4j
@RequestMapping("/lsy")
public class WorkForcesController {
    @Resource
    private WorkForcesService workForcesService;

    @RequestMapping("/manage")
    public String tomanage(){
        return "hospital/manage.html";
    }
    /**
     * 跳转到医院简介页面
     * @return
     */
    @RequestMapping("/profile")
    public String toprofile(){
        return "hospital/profile.html";
    }

    /**
     * 跳转到医院领导页面
     * @return
     */
    @RequestMapping("/expert")
    public String toexpert(){
        return "hospital/expert.html";
    }
    /**
     * 跳转到医院文化页面
     * @return
     */
    @RequestMapping("/culture")
    public String toculture(){
        return "hospital/culture.html";
    }
    /**
     * 跳转到医院荣誉页面
     * @return
     */
    @RequestMapping("/honor")
    public String tohonor(){
        return "hospital/honor.html";
    }
    /**
     * 跳转到排班页面
     * @return
     */
    @RequestMapping("/scheduling")
    public String scheduling(){
        return "doctor/scheduling.html";
    }
    /**
     * 根据科室编号，查询该科室所有医生排班信息
     * @param workforcesDepartment
     * @param resultMap
     * @return
     */
    @RequestMapping("/getWorkForcesList")
    @ResponseBody
    public Object getWorkForcesList(@RequestParam(value = "workforcesDepartment",required = false)Integer workforcesDepartment,
                                    String currentPage,
                                    Map<String,Object>resultMap){
        log.info("查询排班开始");
        //当前页
        int current=0;
        if (currentPage!=null){
            try {
                current=Integer.valueOf(currentPage);
            }catch (NumberFormatException e){
                e.printStackTrace();
                log.info("解析当前页时发生异常");
            }
        }
        //总数量
        int totalCount=workForcesService.getWorkCount(workforcesDepartment);
        if (totalCount==0){
            Map<String,Object>result= ResultUtil.success("查询不到排班信息");
            resultMap.putAll(result);
            resultMap.put("workforcesDepartment",workforcesDepartment);
            return "getWorkForcesList";
        }
        //总页面
        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(current);
        pages.setPageSize(Constants.PAGE_SIZE);
        pages.setTotalCount(totalCount);
        int totalPageCount=pages.getTotalPageCount();

        //控制首页和尾页
        if (current<1){
            current=1;
        }else if (current>totalPageCount){
            current=totalPageCount;
        }
        Map<String,Object> result=workForcesService.getWorkForcesList(workforcesDepartment,current);
        resultMap.putAll(result);
        resultMap.put("totalPageCount",totalPageCount);
        resultMap.put("totalCount",totalCount);
        resultMap.put("current",current);
        resultMap.put("workforcesDepartment",workforcesDepartment);
        log.info("result"+result);
        return result;
    }

    /**
     * 根据医生姓名和科室id查询医生个人的排班信息
     * @param workforcesDoctor
     * @param workforcesDepartment
     * @param resultMap
     * @return
     */
    @RequestMapping("/getWorkBydName")
    @ResponseBody
    public Object getWorkBydName(@RequestParam(value = "workforcesDoctor",required = false)Integer workforcesDoctor,
                                 @RequestParam(value = "workforcesDepartment",required = false)Integer workforcesDepartment,
                                 Map<String,Object>resultMap){
        log.info("查询医生个人排班开始");
        Map<String,Object> result=workForcesService.getWorkBydName(workforcesDoctor,workforcesDepartment);
        resultMap.putAll(result);
        resultMap.put("workforcesDoctor",workforcesDoctor);
        resultMap.put("workforcesDepartment",workforcesDepartment);
        return result;
    }
    @RequestMapping("/togetWorkForcesBydid")
    public String togetWorkForcesBydid(Integer workforcesDoctor,Model model){
        model.addAttribute("workforcesDoctor",workforcesDoctor);
        return "doctor/doctorScheduling";
    }
}
